<template>
  <page-container>
    <app-card>
      <template #header>
        数据筛选
      </template>
      <el-form
        ref="form"
        :inline="true"
        :model="listParams"
        :disabled="listLoading"
        label-width="70px"
        @submit.prevent="handleQuery"
      >
        <el-form-item label="规格名称">
          <el-input
            v-model="listParams.rule_name"
            placeholder="请输入规格名称关键字"
            clearable
            @change="loadList"
            style="width: 300px;"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="loadList"
              />
            </template>
          </el-input>
        </el-form-item>
      </el-form>
    </app-card>
    <app-card>
      <template #header>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="formVisible = true"
        >
          添加商品规格
        </el-button>
        <el-button
          type="warning"
          :loading="deleteRulesLoading"
          @click="handleDeleteRules"
        >
          批量删除
        </el-button>
      </template>
      <el-table
        :data="list"
        v-loading="listLoading"
        style="width: 100%"
        select
        @selection-change="handleSelectionChange"
        row-key="cate_name"
      >
        <el-table-column
          type="selection"
          width="50"
        />
        <el-table-column
          prop="id"
          label="ID"
          min-width="60"
        />
        <el-table-column
          prop="rule_name"
          label="规格名称"
        />

        <el-table-column
          prop="attr_name"
          label="商品规格"
        />

        <el-table-column
          prop="attr_value"
          label="商品属性"
        />

        <el-table-column
          min-width="120px"
          label="操作"
          fixed="right"
          align="center"
        >
          <template #default="{ row }">
            <el-button
              @click="handleEdit(row.id)"
              type="text"
            >
              编辑
            </el-button>
            <el-popconfirm
              title="确定要删除规格吗？（删除后不可恢复哦）"
              @confirm="handleDelete(row.id)"
            >
              <template #reference>
                <el-button type="text">
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <app-pagination
        v-model:page="listParams.page"
        v-model:limit="listParams.limit"
        :list-count="listCount"
        :load-list="loadList"
        :disabled="listLoading"
      />
    </app-card>
  </page-container>
  <attr-form
    v-model="formVisible"
    v-model:rule-id="ruleId"
    @success="handleFormSuccess"
  />
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getRules, removeRule } from '@/api/product'
import { ElMessage } from 'element-plus'
import type { Rule, RuleListParams } from '@/api/types/product'
import AttrForm from './AttrForm.vue'

const list = ref<Rule[]>([])
const listCount = ref(0)
const listLoading = ref(false)
const listParams = reactive<RuleListParams>({
  page: 1,
  rule_name: undefined,
  limit: 15
})

const formVisible = ref(false)
const ruleId = ref<number | undefined>(undefined)

onMounted(() => {
  loadList()
})

const loadList = async () => {
  listLoading.value = true
  const data = await getRules(listParams).finally(() => {
    listLoading.value = false
  })
  data.list.forEach(item => {
    item.statusLoading = false
  })
  list.value = data.list
  listCount.value = data.count
}

const handleQuery = () => {
  listParams.page = 1
  loadList()
}

const handleDelete = async (id: number) => {
  await removeRule({ ids: `${id}` })
  ElMessage.success('删除成功')
  loadList()
}

const handleEdit = (id: number) => {
  ruleId.value = id
  formVisible.value = true
}

const handleFormSuccess = () => {
  formVisible.value = false
  loadList()
}
const deleteRulesLoading = ref(false)

const selectionItems = ref<Rule[]>([])

const handleSelectionChange = (selectedRules: Rule[]) => {
  selectionItems.value = selectedRules
}

const handleDeleteRules = async () => {
  if (!selectionItems.value.length) {
    return ElMessage.warning('请选择规格')
  }
  deleteRulesLoading.value = true
  await removeRule({ ids: `${selectionItems.value.map(item => item.id)}` })
  ElMessage.success('删除成功')
  deleteRulesLoading.value = false

  loadList()
}
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.demo-table-expand {
  font-size: 0;
  :deep(label) {
    width: 90px;
    color: #99a9bf;
  }
  :deep(.el-form-item) {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
}

  </style>
